package com.leetcode.tree;

import com.leetcode.basic.TreeNode;

/**
 * @author Dennis Li
 * @date 2020/9/11 16:51
 */
public class MaxPathSum_124 {

    private int max = Integer.MIN_VALUE;

    public int maxPathSum(TreeNode root) {
        if (root == null) return 0;
        getMax(root);
        return max;
    }

    private int getMax(TreeNode root) {
        if (root == null) return 0;
        int left = getMax(root.left);
        left = Math.max(left, 0);
        int right = getMax(root.right);
        right = Math.max(right, 0);
        max = Math.max(max, left + right + root.val);
        return root.val +  Math.max(left, right);
    }

}
